<?php

namespace app\api\controller\courses;

use app\common\controller\Api;
use think\Config;

/**
 * 新闻资讯
 */
class News extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

        $this->newscategory=model('admin/courses/Newscategory');
        $this->news=model('admin/courses/News');

    }
    /**
     * 新闻分类
     * @param int $pid
     */
    public function category(){
        $this->success('OK',$this->newscategory->order('weigh asc')->select());
    }

    /**
     * 新闻列表
     */
    public function newsList($page=1,$limit=10,$courses_newscategory_id=null){
       $where=[];
        if($courses_newscategory_id!=null){
      $where['courses_newscategory_id']=['=',$courses_newscategory_id];
       }
        $where['status']=['=','normal'];
        $newsList=$this->news->where($where)->paginate($limit,false);
        foreach ($newsList as $key=>$value){
            $newsList[$key]['year-month']=date("Y-m",$value['createtime']);
            $newsList[$key]['day']=date("d",$value['createtime']);
        }
        $this->success('OK',$newsList);
    }

    /**
     * 资讯详情
     */
    public function NewsDetails($id){
        $where['id']=['=',$id];
        $newsInfo= $this->news->get($where);
        if($newsInfo){
            $newsInfo['courses_newscategory_name']=$this->newscategory->where('id',$newsInfo->courses_newscategory_id)->value('name');
            $newsInfo['getPrevArticle']=$this->getPrevArticle($id);
            $newsInfo['getNextArticle']=$this->getNextArticle($id);
            $newsInfo['createtime']=date('Y-m-d H:i:s',$newsInfo['createtime']);
            $this->success('OK',$newsInfo);
        }else{
            $this->error('未找到数据');
        }
    }

    /**
     * 上一篇
     * @param $id
     * @return null
     */
    public function getPrevArticle($id)
    {
        $where['id']=['=',$id];

        $article= $this->news->get($where);
        if($article){
            $prevArticle = $this->news->where('courses_newscategory_id', '=', $article->courses_newscategory_id)
                ->where('createtime', '<', $article->createtime)
                ->where('status','normal')
                ->order('createtime', 'desc')
                ->field('id,title')
                ->limit(1)
                ->find();
        }else{
            $prevArticle=null;
        }

        return $prevArticle;
    }

    // 获取下一篇文章
    public function getNextArticle($id)
    { $where['id']=['=',$id];

        $article= $this->news->get($where);

        if($article){
            $nextArticle = $this->news->where('courses_newscategory_id', '=', $article->courses_newscategory_id)
                ->where('createtime', '>', $article->createtime)
                ->where('status','normal')
                ->order('createtime', 'desc')
                ->field('id,title')
                ->limit(1)
                ->find();

        } else{
            $nextArticle= null;
        }
        return $nextArticle;

    }
}
